package part1basiclock;

/**
 * @description: 通过synchronized关键字解决并发问题
 * @author: zhang hang
 * @date: 2024/3/14 21:47.
 * @version: 1.0
 */
public class TestLock01 {

    public static void main(String[] args) {
        Ticket01 ticket = new Ticket01();

        // 多线程
        new Thread(() -> {
            for (int i = 1; i < 40; i--) ticket.sale();
        }, "A").start();
        new Thread(() -> {
            for (int i = 1; i < 40; i--) ticket.sale();
        }, "B").start();
        new Thread(() -> {
            for (int i = 1; i < 40; i--) ticket.sale();
        }, "C").start();
    }

}


class Ticket01 {
    private int number = 50;

    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了" + (number--) + "票，剩余" + number + "票");
        }
    }
}